package com.net.oil.main.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.main.bean.NewAccountCoupon;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2019/4/21 0021:上午 10:09
 * 邮箱：245850413@qq.com
 * 说明：
 */
public class CouponRecyclerViewAdapter extends RecyclerView.Adapter<CouponRecyclerViewAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<NewAccountCoupon> datas;
    private OnGetCoupon onGetCoupon;

    public CouponRecyclerViewAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        datas = new ArrayList<>();
    }

    public void updateDatas(List<NewAccountCoupon> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    public void setOnGetCoupon(OnGetCoupon onGetCoupon) {
        this.onGetCoupon = onGetCoupon;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.coupon_recycler_view_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final NewAccountCoupon newAccountCoupon = datas.get(position);
        if (newAccountCoupon != null){
            holder.amountTv.setText(String.valueOf(newAccountCoupon.getAmount()));
            String startTime = newAccountCoupon.getCouponValidSdate();
            String endTime = newAccountCoupon.getCouponValidEndDate();
            if (TextUtils.isEmpty(startTime)){
                startTime = "无";
            }
            if (TextUtils.isEmpty(endTime)){
                endTime = "无";
            }
            holder.timeTv.setText(String.format(context.getString(R.string.start_and_end_time), startTime, endTime));
            holder.getTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onGetCoupon.getCoupon(newAccountCoupon.getId());
                }
            });

            if (newAccountCoupon.getAvailableAmount() != null){
                if (newAccountCoupon.getAvailableAmount() > 0){
                    holder.useConditionTv.setVisibility(View.VISIBLE);
                    holder.useConditionTv.setText(String.format(context.getString(R.string.use_condition), newAccountCoupon.getAvailableAmount()));
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas != null ? datas.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.amount_tv)
        TextView amountTv;
        @BindView(R.id.use_condition_tv)
        TextView useConditionTv;
        @BindView(R.id.get_tv)
        TextView getTv;
        @BindView(R.id.time_tv)
        TextView timeTv;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnGetCoupon{
        /**
         * 领取优惠券按钮点击回调
         * @param id    优惠券id
         */
        void getCoupon(int id);
    }
}
